package com.zhengke.service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zhengke.common.util.LoadProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhengke.common.dao.CommonDao;
import com.zhengke.common.object.Result;
import com.zhengke.pojo.Typgl;

@Service
public class TypglServiceImpl implements TypglService {
    @Autowired
    private CommonDao dao;

    @Override
    public Result queryTypgls(String goods_name, String supplier_name, String area_name, int pageNum, int size) throws Exception {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("goods_name", goods_name);
        param.put("supplier_name", supplier_name);
        param.put("area_name", area_name);
        PageHelper.startPage(pageNum, size, true);
        List<Typgl> rtnList = (List<Typgl>) dao.findForList("TypglMapper.querys", param);

        PageInfo<Typgl> pageInfo = new PageInfo<Typgl>(rtnList);

        Result rs = new Result();
        rs.setCode("0");
        rs.setCount(pageInfo.getTotal());
        rs.setData(rtnList);
        return rs;
    }

    @Override
    public Result updateTypgls(Typgl typgl) throws Exception {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("goods_id", typgl.getGoods_id());
        param.put("goods_name", typgl.getGoods_name());
        param.put("common_price", typgl.getCommon_price());
        param.put("specifications", typgl.getSpecifications());
        param.put("shipping_fee", typgl.getShipping_fee());
        param.put("awarded_integral", typgl.getAwarded_integral());
        dao.update("TypglMapper.updates", param);
        return null;
    }

    /**
     * 查询图片信息
     *
     * @param goodId
     * @return
     * @throws Exception
     */
    public List<Typgl> queryPics(String goodId) throws Exception {

        String fileServerUrl = LoadProperties.findValue("FILE_SERVER_URL");
        List<Typgl> rtnList = (List<Typgl>) dao.findForList("TypglMapper.queryPics", Integer.parseInt(goodId));

        return rtnList;
    }

    /**
     * 修改图片
     *
     * @param typgl
     * @return
     */
    @Override
    public int updatePic(Typgl typgl) throws Exception {
        Map<String, Object> param = new HashMap<String, Object>();

        String picPath1 = typgl.getPicPath1();
        String picPath2 = typgl.getPicPath2();
        String picPath3 = typgl.getPicPath3();
        String picPath4 = typgl.getPicPath4();
        String picPath5 = typgl.getPicPath5();
        String thumb_picPath = typgl.getThumb_picPath();
        String info_picPath = typgl.getInfo_picPath();
        if (!"".equals(picPath1) && picPath1 != null) {
            param.put("picPath1", picPath1);
        }
        if (!"".equals(picPath2) && picPath2 != null) {
            param.put("picPath2", picPath2);
        }
        if (!"".equals(picPath3) && picPath3 != null) {
            param.put("picPath3", picPath3);
        }
        if (!"".equals(picPath4) && picPath4 != null) {
            param.put("picPath4", picPath4);
        }
        if (!"".equals(picPath5) && picPath5 != null) {
            param.put("picPath5", picPath5);
        }
        if (!"".equals(thumb_picPath) && thumb_picPath != null) {
            param.put("thumb_picPath", thumb_picPath);
        }
        if (!"".equals(info_picPath) && info_picPath != null) {
            param.put("info_picPath", info_picPath);
        }

        param.put("goods_id", typgl.getGoods_id());

        int i = dao.update("TypglMapper.updatePic", param);
        return i;
    }


}
